package com.jty.lib_base.room

import android.content.Context
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

/**
 *
 * 只能有这几种类型
 * NULL: 表示该值为NULL值。
INTEGER: 无符号整型值。
REAL: 浮点值。
TEXT: 文本字符串，存储使用的编码方式为UTF-8、UTF-16BE、UTF-16LE。
BLOB: 存储Blob数据，该类型数据和输入数据完全相同。
 */


object RoomBuilder {
    //数据库升级
    /*val MIGRATION_1_2: Migration = object: Migration(1, 2){
        override fun migrate(database: SupportSQLiteDatabase) {
            // Create the new table
            database.execSQL(
                "CREATE TABLE user2_new (userid INTEGER, name TEXT, PRIMARY KEY(userid))");

            // Copy the data
            database.execSQL(
                "INSERT INTO user2_new (userid, name) SELECT userid, name FROM user2");
            // Remove the old table
            database.execSQL("DROP TABLE user2");
            // Change the table name to the correct one
            database.execSQL("ALTER TABLE user2_new RENAME TO user2_new_2");
        }

    }*/

    /*val MIGRATION_2_3: Migration = object: Migration(2, 3){
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("CREATE TABLE temp_user2 (" +
                    "id INTEGER PRIMARY KEY NOT NULL," +
                    "name TEXT)");
            database.execSQL("INSERT INTO temp_user2 (id, name) " +
                    "SELECT userid, name FROM user2");
            database.execSQL("DROP TABLE user2");
            database.execSQL("ALTER TABLE temp_user2 RENAME TO user2");
        }
    }*/
    var db_name : String? = null
    fun setDbName(name:String) {
        db_name = name;
    }
    fun getDb(context: Context): AppDatabase{
        return db_name.let {
            Room.databaseBuilder(context, AppDatabase::class.java, "db_${db_name}.db")
                .allowMainThreadQueries()
                //.addMigrations(MIGRATION_1_2, MIGRATION_2_3)
                .build()
        }
    }
}